<?php

namespace App\Commons\Domain\Module\UserCenter\Entity;

use App\Commons\Domain\Core\DomainEntity;
use App\Commons\Domain\Module\UserCenter\Model\Account;
use App\Commons\Domain\Module\UserCenter\Model\Member;
use Hash;

class UserEntity extends DomainEntity
{
    public const ENTITY = Member::class;

    /**
     * 创建用户
     * @param string $username 用户名
     * @param string $password 登录密码
     * @param string $mobile 手机号码
     * @param int $country_id 国家id
     * @param string $email 电子邮箱
     * @param int $type
     * @return mixed
     */
    public function createUser(
        string $username,
        string $password,
        string $mobile,
        int $country_id,
        string $email = '',
        int $type = 1
    ) {
        $password = Hash::make($password);

        return db_trans(function () use ($email, $mobile, $password, $username, $country_id, $type) {
            if ($mobile) {
                throw_on($this->whereMobile($mobile)->exists(), 0xf00262);
            }
            if ($email) {
                throw_on($this->whereEmail($email)->exists(), 0xf00102);
            }
            throw_on($this->whereUsername($username)->exists(), 0xf00112);

            $user_data = compact('username', 'password', 'mobile', 'email', 'country_id', 'type');

            $user = $this->create($user_data);
            $user->account()->create($user_data);

            return $user;
        });
    }

    /**
     * 重设登录密码
     * @param string $mobile 手机号码
     * @param string $new_password 新密码
     * @param string $email
     * @return mixed
     */
    public function forgotPassword(string $mobile, string $new_password, string $email)
    {
        if (!empty($mobile)) {
            $account = throw_empty(Account::whereMobile($mobile)->first(['id', 'uid', 'password']), 0xf00122);
        } else {
            $account = throw_empty(Account::whereEmail($email)->first(['id', 'uid', 'password']), 0xf00122);
        }

        $account->password = Hash::make($new_password);
        return $account->save();
    }

    /**
     * 修改密码
     * @param $user
     * @param string $new_password 新密码
     * @return mixed
     */
    public function updatePassword($user, string $new_password)
    {
        return $user->account()->update(
            [
                'password' => Hash::make($new_password),
            ]
        );
    }

    /**
     * 修改通知状态
     * @param Member $user
     * @param string $field
     * @param int $status
     * @return bool
     */
    public function switchNotice(Member $user, string $field, int $status)
    {
        return $user->update(
            [
                $field => $status
            ]
        );
    }

    /**
     * 修改用户信息
     * @param Member $user 登陆用户
     * @param string $nickname 昵称
     * @param string $avatar 头像
     * @param int $gender 性别：0-未知，1-男，2-女
     * @return bool
     */
    public function updateUserInfo(Member $user, string $nickname, string $avatar, int $gender)
    {
        return $user->update(compact('nickname', 'avatar', 'gender'));
    }

    /**
     * 更新用户登录信息
     * @param Member $user 登录用户
     * @param string $ip 登录ip
     * @return mixed
     */
    public function updateUserLogin($user, string $ip)
    {
        return db_trans(
            function () use ($user, $ip) {
                $user->account->update(
                    [
                        'last_logined_at' => now(),
                        'last_logined_ip' => $ip
                    ]
                );
                return $user->account->increment('logined_count');
            }
        );
    }

    /**
     * 修改资金密码
     * @param Member $member
     * @param string $password
     * @return bool
     */
    public function changePwd(Member $member, string $password)
    {
        $member->jy_password = Hash::make($password);
        return $member->save();
    }
}
